//
//  HTLicenseBoardView.m
//  TruckDriver
//
//  Created by Ting Han on 2017/10/24.
//  Copyright © 2017年 Ting Han. All rights reserved.
//

#import "HTLicenseBoardView.h"
#import "HTNormalCollectionCell.h"

@interface HTLicenseBoardView()<UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>

@property (weak, nonatomic) IBOutlet UICollectionView *collectionView;
@property (weak, nonatomic) IBOutlet UICollectionViewFlowLayout *flowLayout;

@property (nonatomic,strong) NSArray *licenses;
@property (nonatomic,strong) NSArray *licenseNumber;

@property (nonatomic,strong) NSArray *dataSource;

@end

@implementation HTLicenseBoardView

- (void)awakeFromNib{
    [super awakeFromNib];
    [self setupData];
    [self configCollectionView];
}

- (void)setupData{
    NSString *path = [[NSBundle mainBundle] pathForResource:@"Trucks" ofType:@"plist"];
    NSDictionary *plistData =[NSDictionary dictionaryWithContentsOfFile:path];
    self.licenses = plistData[@"TruckLicense"];
    self.licenseNumber = plistData[@"TruckLicenseNumber"];
    self.dataSource = self.licenses;
    self.licenseLevel = LicenseBoardLevelOne;
}
- (void)configCollectionView{
    [self.flowLayout setScrollDirection:UICollectionViewScrollDirectionVertical];
    CGFloat width = (kHTScreenWidth)/6.0;
    self.flowLayout.itemSize = CGSizeMake(width, (kHTScreenWidth)/6.0 - kWidth(20));
    self.flowLayout.sectionInset = UIEdgeInsetsMake(0, 0, 5, 0);
    self.flowLayout.minimumLineSpacing = 0;
    self.flowLayout.minimumInteritemSpacing = 0;
    self.collectionView.backgroundColor = [UIColor whiteColor];
    [self.collectionView registerNib:[UINib nibWithNibName:@"HTNormalCollectionCell" bundle:nil] forCellWithReuseIdentifier:normalCollectionCell];
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
}
- (void)setLicenseLevel:(LicenseBoardLevel)licenseLevel{
    _licenseLevel = licenseLevel;
    if (licenseLevel == LicenseBoardLevelOne) {
        self.dataSource = self.licenses;
        [self.collectionView reloadData];
    }
    if (licenseLevel == LicenseBoardLevelTwo) {
        self.dataSource = self.licenseNumber;
        [self.collectionView reloadData];
    }
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return 1;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.dataSource.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    HTNormalCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:normalCollectionCell forIndexPath:indexPath];
    cell.titleLabel.font = [UIFont systemFontOfSize:18];
    cell.titleLabel.text = self.dataSource[indexPath.row];
    return cell;
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    if (self.delegate && [self.delegate respondsToSelector:@selector(didChoiceItemIndex:withTitle:)]) {
        [self.delegate didChoiceItemIndex:indexPath.row withTitle:self.dataSource[indexPath.row]];
    }
}

@end
